Ext.require(['Ext.data.Store']);
//Ext.require(['Ext.data.JsonStore']);

(function( vvg){
   
   vvg.app =  vvg.app || {};
   var app =  vvg.app;
   var lib =  vvg.lib;
   var libBore = lib.bore;

   app.generateSampleWFSData = function(){
      var myTransect = libBore.boreTransect(libBore.defaultBoreDescriptor);

      var i,
         bore,
         dates = ['2010-01-02 9:22', '2011-02-03 12:01',  '2012-03-04 22:24'];
//      Math.seedrandom('fdsafjklwej');
      for (i = 0; i<18; i += 1) {
         bore = {};
         bore.label = 'Bore ' + i.toString();
         bore.linearDistance = Math.random()*120 - 20;
//         bore.linearDistance = Math.random()*12000.0 - 2000;
//         bore.linearDistance = Math.random()*10000.0;
//         bore.linearDistance = i*1000.0;
         bore.elevation = Math.random()*100.0+200.0;
         bore.offset = Math.random()*1000.0-500.0;
         bore.depth = Math.random()*80.0+10.0;
         bore.groundwaterLevel = Math.max(bore.depth-(Math.random()*10.0), 0.0);
         //bore.groundwaterLevelDate = dates[Math.floor(Math.random()*(dates.length))];
         bore.groundwaterLevelDate = dates[i % dates.length];
         bore.eh = Math.random()*0.1;
         // Make some bores incomplete:
//         if (i === 0 || i === 1) {
//            delete bore.label;
//            bore.elevation = 0.0;
//         }
         myTransect.addBore(bore);
      }
      
      return myTransect.getBores();
   };
   
//   app.generateSampleAreaChartData = function() {
//      var i = 0;
//      var bores = app.generateSampleWFSData();
//      for(i = 0; i < bores.length; i++) {
//         bores[i].depth = bores[i].elevation - bores[i].depth;
//         bores[i].groundwaterLevel = bores[i].elevation - bores[i].groundwaterLevel;
//         bores[i].linearDistance = Math.round(bores[i].linearDistance);
//         
//         lib.log(
//         '('+
//            bores[i].linearDistance +', '+
//            bores[i].elevation +', '+ 
//            bores[i].groundwaterLevel +', '+ 
//            bores[i].depth +')');
//      }
//      
//      return bores;
// 
//   };

   app.generateSampleAreaChartData = function() {
      var i = 0;
      var bores = app.generateSampleWFSData();
      for(i = 0; i < bores.length; i++) {
         // simplify display of x-axis values
         bores[i].linearDistance = Math.round(bores[i].linearDistance);
         
         // adjust all to same reference point (DEM)
         bores[i].depth = bores[i].elevation - bores[i].depth;
         bores[i].groundwaterLevel = bores[i].elevation - bores[i].groundwaterLevel;
         
         // cumulative values for stacked area chart
         bores[i].depth_a = bores[i].depth;
         bores[i].groundwaterLevel_a = bores[i].groundwaterLevel - bores[i].depth;
         bores[i].elevation_a = bores[i].elevation - bores[i].groundwaterLevel;
         
         
         lib.log(
         '('+
            bores[i].linearDistance +',\n\t'+
            bores[i].depth +', '+
            bores[i].groundwaterLevel +', '+
            bores[i].elevation +',\n\t'+
            bores[i].depth_a +', '+
            bores[i].groundwaterLevel_a +', '+
            bores[i].elevation_a
            +')');
      }
      
      return bores;
 
   };

   Ext.define('VVG.store.BoreData', {
      extend: 'Ext.data.Store'
      ,fields: [
         'linearDistance', 'elevation', 'depth', 'groundwaterLevel'
         , 'depth_a', 'groundwaterLevel_a', 'elevation_a'
      ]
      ,data: app.generateSampleAreaChartData()
//      ,data: app.generateData()
   });
   
}(window.vvg = window.vvg || {}));
